#include "volumetool.h"
#include "ui_volumetool.h"
#include<QGraphicsDropShadowEffect>
#include<QPainter>
#include<QPolygon>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    volumeRatio(20),
    isMuted(false)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);

    //设置窗口背景透明
    // 在windows上，设置透明效果后，窗⼝需要加上Qt::FramelessWindowHint格式,否则没有控件位置的背景是⿊⾊的
    // 由于默认窗⼝有阴影，因此还需要将窗⼝的原有的阴影去掉，窗⼝需要加上Qt::NoDropShadowWindowHint
    this->setAttribute(Qt::WA_TranslucentBackground);

    //自定义阴影效果
    QGraphicsDropShadowEffect* dropShadow=new QGraphicsDropShadowEffect(this);
    dropShadow->setOffset(0,0);
    dropShadow->setBlurRadius(10);
    dropShadow->setColor("#646464");
    this->setGraphicsEffect(dropShadow);

    ui->silenceBtn->setIcon(QIcon(":/image/volume.png"));
    ui->volumeRatio->setText("20%");
    ui->outSlider->setGeometry(ui->outSlider->x(),180-36+25,ui->outSlider->width(),36);
    ui->sliderBtn->move(ui->sliderBtn->x(),ui->outSlider->y()-ui->sliderBtn->height()/2);
    ui->sliderBox->installEventFilter(this);
    connect(ui->silenceBtn,&QPushButton::clicked,this,&VolumeTool::onSilenceBtnClicked);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::setVolume()
{
    int height=ui->sliderBox->mapFromGlobal(QCursor().pos()).y();

    height=height<25?25:height;
    height=height>205?205:height;

    ui->outSlider->setGeometry(ui->outSlider->x(),height,ui->outSlider->width(),205-height);
    ui->sliderBtn->move(ui->sliderBtn->x(),ui->outSlider->y()-ui->sliderBtn->height()/2);
    volumeRatio=(int)(ui->outSlider->height()/(float)180*100);
    ui->volumeRatio->setText(QString::number(volumeRatio)+"%");
}

void VolumeTool::onSilenceBtnClicked()
{
    isMuted=!isMuted;
    if(isMuted)
    {
        ui->silenceBtn->setIcon(QIcon(":/image/silence.png"));
    }
    else
    {
        ui->silenceBtn->setIcon(QIcon(":/image/volume.png"));
    }
    emit setSilence(isMuted);
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    (void)event;
    //设置绘图对象
    QPainter painter(this);
    //设置画笔,没有画笔时：画出来的图形就没有边框和轮廓线
    painter.setPen(Qt::NoPen);
    //设置画刷颜色
    painter.setBrush(Qt::white);

    //创建一个三角形
    QPolygon polygon;
    polygon.append(QPoint(10+10,300));
    polygon.append(QPoint(10+10+60,300));
    polygon.append(QPoint(10+10+30,300+20));

    painter.drawPolygon(polygon);

}

bool VolumeTool::eventFilter(QObject *object, QEvent *event)
{
    if(object==ui->sliderBox)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            setVolume();
        }
        else if(event->type()==QEvent::MouseMove)
        {
            emit setMusicVolume(volumeRatio);
        }
        else if(event->type()==QEvent::MouseButtonRelease)
        {
            setVolume();
            emit setMusicVolume(volumeRatio);
        }
        return true;
    }
    return QObject::eventFilter(object, event);
}
